package com.kdtech.analyse.news;
import com.alibaba.fastjson.JSONObject;
import com.kdtech.utils.DateUtils;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DoMainUtils;

public class Nscc_gzCnNewsAnalyse implements AnalyseNews {
	private static final String[] regex={
		"http://www.nscc-gz.cn/*",
		"http://www.nscc-gz.cn/newsdetail.html[?][0-9]*",
		"http://www.nscc-gz.cn/[a-z]*/[a-z]*.html",
		"http://www.nscc-gz.cn/[a-zA-Z]*/[a-zA-Z]*/[a-zA-Z]*.html",
		"http://www.nscc-gz.cn/Apps/[a-zA-Z]*.html",
		"http://www.nscc-gz.cn/[a-zA-Z]*.html",
		"http://www.nscc-gz.cn/Service/[a-zA-Z]*.html"
	};
	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}
	
	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		String htmltxt = urlMeta.getHtml();
		String url=urlMeta.getUrl();
		if(!isDetailPage(url)){
		}				
		
		
		String title=null;
		String content=null;
		Long date=null;
		String author = null;
		if(url.matches("http://www.nscc-gz.cn/newsdetail.html\\?[0-9]+")){
			String id = StringUtils.substringAfter(url, "?");
			url = "http://www.nscc-gz.cn/api/article/" + id;
			urlMeta = CrawlHTML.responseToURL(url);
			if(urlMeta!=null){
				htmltxt = urlMeta.getHtml();
			}
			try{
				JSONObject jsonObj = JSONObject.parseObject(htmltxt);
				title = jsonObj.getString("Title");
				date = DateUtils.matchDate(jsonObj.getString("CreatedDate"));
				content = jsonObj.getString("Content");
				author = jsonObj.getString("CreatorName");
			}catch(RuntimeException e){
				e.printStackTrace();
			}
		}else{
			Document doc=Jsoup.parse(htmltxt);
			title=doc.select("title").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("div.article-detail"));
			date =JSoupUtils.matchDate(doc, "查看","回复","阅读","发表于", "发布时间：", "上传日期");
		}
		
		if(StringUtils.isBlank(title)){
		}
		news.setUrl(url);		
		news.setTitle(title);
		news.setContent(content);
		news.setDate(date);
		news.setAuthor(author);
		return news;
	}
	
	public static void main(String[] args) {
		System.out.println(DoMainUtils.GetDomainName("http://www.nscc-gz.cn/index.html"));
		Nscc_gzCnNewsAnalyse test=new Nscc_gzCnNewsAnalyse();
		String url="http://www.nscc-gz.cn/newsdetail.html?7472";
		System.out.println(test.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=test.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
